package com.sj.uabrowser.mvp.video.videoutils;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.orhanobut.logger.Logger;
import com.shuyu.gsyvideoplayer.utils.GSYVideoHelper;
import com.sj.uabrowser.BuildConfig;
import com.sj.uabrowser.R;
import com.sj.uabrowser.mvp.video.bean.NVideoItem;
import com.sj.uabrowser.mvp.video.view.adp.RecyclerBaseAdapter;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by GUO on 2015/12/3.
 */
public class RecyclerItemViewHolder extends RecyclerItemBaseHolder implements RecyclerBaseAdapter.VideoItemClickListener {

    public final static String TAG = "RecyclerView2List";

    protected Context context = null;

    @BindView(R.id.list_item_container)
    FrameLayout listItemContainer;

    @BindView(R.id.list_item_btn)
    ImageView listItemBtn;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.iv_avatar)
            ImageView ivAvatar;
    @BindView(R.id.tv_time)
            TextView tv_time;
    @BindView(R.id.tv_pushname)
            TextView tvpushname;
    @BindView(R.id.tv_videoBrowse)
            TextView tvVideoBrose;
    @BindView(R.id.rl_content)
    RelativeLayout rlContent;
    @BindView(R.id.rl_share)
    View ib_share;


//    helper.setText(R.id.tv_title,item.getVideoTitle());
//        helper.setText(R.id.tv_time,item.getVideoTime());
//        helper.setText(R.id.tv_pushname,item.getPushName());
//        Glide.with(mContext).load(item.getAvaterImg()).into((ImageView) helper.getView(R.id.iv_avatar));

    ImageView imageView;

    private GSYVideoHelper smallVideoHelper;

    private GSYVideoHelper.GSYVideoHelperBuilder gsySmallVideoHelperBuilder;

    public RecyclerItemViewHolder(Context context, View v) {
        super(v);
        this.context = context;
        ButterKnife.bind(this, v);
        imageView = new ImageView(context);
    }

    public interface ShareListener{
        void onShareItemClick(int position);
    }

    public interface UpdateUrlListener {
        void onUpdateUrl(int position);
    }

    private ShareListener shareListener;
    private UpdateUrlListener updateUrlListener;

    public void setUpdateUrlListener(UpdateUrlListener updateUrlListener) {
        this.updateUrlListener = updateUrlListener;
    }

    public void setShareListener(ShareListener shareListener) {
        this.shareListener = shareListener;
    }

    boolean playFlag  = false;

    public void onBind(final int position, NVideoItem videoItem) {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(context, VideoDetailAct.class);
//                intent.putExtra("data",videoItem);
//                context.startActivity(intent);
            }
        };
        tv_title.setOnClickListener(listener);
        rlContent.setOnClickListener(listener);
        ib_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(shareListener!=null){
                    shareListener.onShareItemClick(position);
                }
            }
        });
        tv_title.setText(videoItem.getTitle());
        tv_time.setText(videoItem.getVideo_duration_str());
        tvpushname.setText(videoItem.getSource());
        tvVideoBrose.setText(videoItem.getVideo_play_count()+"次播放");
        Glide.with(context).load(videoItem.getMedia_avatar_url()).into(ivAvatar);
        //增加封面
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//        imageView.setImageResource(R.mipmap.xxx1);

        Glide.with(context).load(videoItem.getImage_url()).apply(new RequestOptions().placeholder(R.drawable.common_list_item_nodata).error(R.mipmap.ic_error)).into(imageView);

        smallVideoHelper.addVideoPlayer(position, imageView, TAG, listItemContainer, listItemBtn);

        listItemBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = videoItem.getVideo_url();
                playFlag=false;
                onVideoItemClick(url,position);
                //必须在startPlay之后设置才能生效
//                listVideoUtil.getGsyVideoPlayer().getTitleTextView().setVisibility(View.VISIBLE);
            }


        });
    }

    public void onVideoItemPause(){
        Logger.e("item video hidden.");
        smallVideoHelper.getGsyVideoPlayer().onVideoPause();
    }

    @Override
    public void onVideoItemClick(String url,int position) {
        smallVideoHelper.setPlayPositionAndTag(position, TAG);
        getRecyclerBaseAdapter().notifyDataSetChanged();
        //listVideoUtil.setLoop(true);
        //listVideoUtil.setCachePath(new File(FileUtils.getPath()));

        gsySmallVideoHelperBuilder.setVideoTitle("title " + position).setUrl(url);

        smallVideoHelper.startPlay();
        tv_time.setVisibility(View.INVISIBLE);
        tv_title.setVisibility(View.INVISIBLE);
        smallVideoHelper.getGsyVideoPlayer().setVideoAllCallBack(new MyVideoAllCallBack(){
            @Override
            public void onPlayError(String url, Object... objects) {
                super.onPlayError(url, objects);
                if(updateUrlListener!=null&&!playFlag){
                    playFlag=true;
                    updateUrlListener.onUpdateUrl(position);
                }
            }

            @Override
            public void onClickStartError(String url, Object... objects) {
                super.onClickStartError(url, objects);
//                Util.showToastShort("该视频已下架！");
            }

            @Override
            public void onClickStartIcon(String url, Object... objects) {
                super.onClickStartIcon(url, objects);
                Logger.e("onClickStartIcon...");
            }

            @Override
            public void onClickResume(String url, Object... objects) {
                super.onClickResume(url, objects);
//                tvVideoBrose.setVisibility(View.INVISIBLE);
                tv_time.setVisibility(View.INVISIBLE);
                tv_title.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onClickStop(String url, Object... objects) {
                super.onClickStop(url, objects);
                Logger.e("onClickStop");
//                tvVideoBrose.setVisibility(View.VISIBLE);
//                tv_time.setVisibility(View.VISIBLE);
                tv_title.setVisibility(View.VISIBLE);
            }
        });
        if(BuildConfig.UM) {
            String eventid= "video-video";
            MobclickAgent.onEvent(context, eventid,"视频-视频");
        }
    }


    public void setVideoHelper(GSYVideoHelper smallVideoHelper, GSYVideoHelper.GSYVideoHelperBuilder gsySmallVideoHelperBuilder) {
        this.smallVideoHelper = smallVideoHelper;
        this.gsySmallVideoHelperBuilder = gsySmallVideoHelperBuilder;
    }

}





